﻿/* 
 * 
 * Author: Martin Paulík
 * 
 */ 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PB138___projekt
{
    /// <summary>
    /// Form for insert media.
    /// </summary>
    public partial class insertMedia : Form
    {
        XML xml = new XML();
        List<Item> listItems = new List<Item>();
        int numberOfItem = 1;
        int currentNumberOfItem = 0;
        public insertMedia()
        {
            InitializeComponent();
            insertToCombobox();
            listItems.Add(new Item("", "", ""));
            comboBox2.SelectedIndex = 0;
        }

        /// <summary>
        /// Insert list of categories to comboBox. 
        /// </summary>
        private void insertToCombobox()
        {
            List<string> categories = xml.getCategories();
            foreach (string category in categories)
                comboBox1.Items.Add(category);
        }

        /// <summary>
        /// Stops showing of errorProvider.
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void comboBox1_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(comboBox1, "");
        }

        /// <summary>
        /// Check if comboBox was selected (or length is not 0).
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">CancelEventArgs</param>
        private void comboBox1_Validating(object sender, CancelEventArgs e)
        {
            if (comboBox1.Text.Length == 0)
            {
                e.Cancel = true;
                comboBox1.Select(0, comboBox1.Text.Length);
                errorProvider1.SetError(comboBox1, "The name of category is required !!");
            }
        }

        /// <summary>
        /// Click button Insert, inserts media to xml file. 
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void button1_Click(object sender, EventArgs e)
        {
            changeItem();
            if (textBox1.Text.Length == 0)
            {
                textBox1_Validating(new object(), new CancelEventArgs());
            }
            else
            {
                int i = 0;
                int index = 0;
                bool empty = false;
                foreach (Item it in listItems)
                {

                    if (it.Name.Length == 0)
                    {
                        empty = true;
                        index = i;
                        
                    }
                    i++;
                }
                if (empty == true)
                {
                    comboBox2.SelectedIndex = index;
                    textBox3_Validating(new object(), new CancelEventArgs());
                }
                else
                {
                    if (comboBox1.Text.Length == 0)
                        comboBox1_Validating(new object(), new CancelEventArgs());
                    else
                    {
                        int category = comboBox1.SelectedIndex + 1;

                        if (xml.insert(category, textBox1.Text, textBox2.Text, listItems) == true)
                            MessageBox.Show("Successful inserting", "SUCCESS", System.Windows.Forms.MessageBoxButtons.OK,
                           System.Windows.Forms.MessageBoxIcon.Information);
                        this.Close();
                    }
                }

            }
        }

        /// <summary>
        /// Check if textBox length is not 0.
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">CancelEventArgs</param>
        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            if (textBox1.Text.Length == 0)
            {
                e.Cancel = true;
                textBox1.Select(0, textBox1.Text.Length);
                errorProvider1.SetError(textBox1, "Name is required.");
            }
        }

        /// <summary>
        /// Check if textBox length is not 0.
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">CancelEventArgs</param>
        private void textBox3_Validating(object sender, CancelEventArgs e)
        {
            if (textBox3.Text.Length == 0)
            {
                e.Cancel = true;
                textBox3.Select(0, textBox3.Text.Length);
                errorProvider1.SetError(textBox3, "Item is required.");
            }
        }

        /// <summary>
        /// Stops showing of errorProvider.
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void textBox1_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(textBox1, "");
        }

        /// <summary>
        /// Stops showing of errorProvider.
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void textBox3_Validated(object sender, EventArgs e)
        {
            errorProvider1.SetError(textBox3, "");
        }

        /// <summary>
        /// Click button Create new item
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox3.Text.Length > 0)
            {
                changeItem();
                numberOfItem++;
                comboBox2.Items.Add(numberOfItem.ToString());
                listItems.Add(new Item("", "", ""));
                currentNumberOfItem = numberOfItem - 1;
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                comboBox2.SelectedIndex = numberOfItem - 1;

            }
            else
            {
                textBox3_Validating(new object(), new CancelEventArgs()); 
            }


        }
        /// <summary>
        /// If user select other index of comboBox, then save to listItems name, year, duration.
        /// Then show name, year, duration of selected index. 
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            changeItem();
            currentNumberOfItem = Convert.ToInt16(((ComboBox)sender).SelectedIndex);
            textBox3.Text = listItems[currentNumberOfItem].Name;
            textBox4.Text = listItems[currentNumberOfItem].Year;
            textBox5.Text = listItems[currentNumberOfItem].Duration;

        }
        /// <summary>
        /// Write or rewrite to item of listItems new name, duration and year.
        /// </summary>
        private void changeItem()
        {
            listItems[currentNumberOfItem].Name = textBox3.Text;
            listItems[currentNumberOfItem].Duration = textBox5.Text;
            listItems[currentNumberOfItem].Year = textBox4.Text;
        }

        /// <summary>
        /// Click button Delete item
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (listItems.Count > 1)
            {
                listItems.RemoveAt(currentNumberOfItem);
                numberOfItem--;
                comboBox2.Items.RemoveAt(currentNumberOfItem);
                if (listItems.Count == currentNumberOfItem)
                {
                    currentNumberOfItem--;
                }
                reloadCombobBox2();
                textBox3.Text = listItems[currentNumberOfItem].Name;
                textBox4.Text = listItems[currentNumberOfItem].Year;
                textBox5.Text = listItems[currentNumberOfItem].Duration;
                comboBox2.SelectedIndex = currentNumberOfItem;
            }
            else
            {
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
                changeItem();
            }
        }

        /// <summary>
        /// Refresh list of number of item. 
        /// </summary>
        private void reloadCombobBox2()
        {
            comboBox2.Items.Clear();
            for (int i = 0; i < listItems.Count; i++)
            {
                comboBox2.Items.Add((i+1).ToString());
            }
        }


        /// <summary>
        /// Click button clear. 
        /// </summary>
        /// <param name="sender">Object</param>
        /// <param name="e">EventArgs</param>
        private void button2_Click(object sender, EventArgs e)
        {
            numberOfItem = 1;
            currentNumberOfItem = 0;
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            comboBox2.Items.Clear();
            comboBox2.Items.Add("1");
            listItems = new List<Item>();
            listItems.Add(new Item("", "", ""));
            comboBox2.SelectedIndex = 0;
            comboBox1.SelectedIndex = -1;
        }


    }
}
